package pl.un4giv3n.mightvsmagic.action.clan;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.User;

public class ChangeClanUserPermissionsPageAction extends ClanAction {

	private static final long serialVersionUID = 1L;
	private Long userId;
	private User selectedUser;

	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		clan = user.getClan();
		if (clan == null || !isCanConfigClan()) {
			entityManager.close();
			addActionError("Wybrany klan nie istnieje lub niewystarczające uprawnienia.");
			return INPUT;
		} else if (userId != null && entityManager.find(User.class, userId) != null) {
			selectedUser = entityManager.find(User.class, userId);
			entityManager.close();
			return SUCCESS;
		} else{
			entityManager.close();
			addActionError("Wybrany użytkownik nie istnieje");
			return INPUT;
		}
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}
	
	public boolean isClanMod(){
		System.out.println("clanMod="+(selectedUser.isClanAdmin() || selectedUser.isClanMod()));
		return (selectedUser.isClanAdmin() || selectedUser.isClanMod());
	}
	@Override
	public boolean isClanAdmin(){
		System.out.println("clanAdmin="+selectedUser.isClanAdmin());
		return selectedUser.isClanAdmin();
	}

}
